package com.jeesite.modules.digitalize.service;

import cn.hutool.core.collection.CollectionUtil;
import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.modules.digitalize.entity.DigitalizeComponent;
import com.jeesite.modules.digitalize.entity.DigitalizeComponentIntegration;
import com.jeesite.modules.digitalize.entity.DigitalizeResourceLibrary;
import com.jeesite.modules.digitalize.entity.dto.RecycleBinDelDTO;
import com.jeesite.modules.digitalize.entity.vo.component.ComponentRecycleVo;
import com.jeesite.modules.digitalize.util.response.ResultVo;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zhaoshuaixiang
 * @date 2024/1/8 10:06
 */
@Service
public class RecyclebinService {

    @Resource
    private DigitalizeComponentService digitalizeComponentService;

    @Resource
    private DigitalizeResourceLibraryService digitalizeResourceLibraryService;

    @Resource
    private DigitalizeComponentIntegrationService digitalizeComponentIntegrationService;

    public ResultVo<List<ComponentRecycleVo>> recycleList(String fileName,String projectCode) {
        //查询列表
        return ResultVo.success(digitalizeComponentService.getRecycleList(fileName,projectCode));
    }

    public ResultVo delFile(List<RecycleBinDelDTO> dtos) {
        //删除version
        if (CollectionUtil.isEmpty(dtos)) {
            return ResultVo.success();
        }
        dtos.stream().filter(item -> item.getFileSource() == 0 || item.getFileSource() == 1).forEach(item -> {
            DigitalizeComponent component = new DigitalizeComponent();
            component.setDelStatus(1);
            component.setUpdateBy(UserUtils.getUser().getId());
            component.setId(item.getId().longValue());
            digitalizeComponentService.updateDel(component);
        });
        dtos.stream().filter(item -> item.getFileSource() == 2).forEach(item -> {
            DigitalizeResourceLibrary library = new DigitalizeResourceLibrary();
            library.setDelStatus(1);
            library.setUpdateBy(UserUtils.getUser().getId());
            library.setId(item.getId().longValue());
            digitalizeResourceLibraryService.updateDel(library);
        });
        dtos.stream().filter(item -> item.getFileSource() == 3).forEach(item -> {
            DigitalizeComponentIntegration integration = new DigitalizeComponentIntegration();
            integration.setDelStatus(1);
            integration.setUpdateBy(UserUtils.getUser().getId());
            integration.setId(item.getId().longValue());
            digitalizeComponentIntegrationService.updateByIdAndDelStatus(integration);
        });
        return null;
    }

    public ResultVo restore(List<RecycleBinDelDTO> dtos) {
        //恢复同一批次
        if (CollectionUtil.isEmpty(dtos)) {
            return ResultVo.success();
        }
        for (RecycleBinDelDTO dto : dtos) {
            //图纸 模型
            if (dto.getFileSource() == 1 || dto.getFileSource() == 0) {
                DigitalizeComponent component = digitalizeComponentService.getByIdAndDelStatus(dto.getId());
                if (component == null) {
                   continue;
                }
                if (component.getParentId() == 0) {
                    //根目录
                    component.setDelStatus(0);
                    component.setUpdateBy(UserUtils.getUser().getId());
                    digitalizeComponentService.updateByIdAndDelStatus(component);
                } else {
                    //子目录 判断上级文件是否存在
                    DigitalizeComponent parentComponent = digitalizeComponentService.getById(component.getParentId());
                    if (ObjectUtils.isEmpty(parentComponent)) {
                        component.setParentId(0);
                    }
                    component.setDelStatus(0);
                    component.setUpdateBy(UserUtils.getUser().getId());
                    component.setUpdateDate(new Date());
                    digitalizeComponentService.updateByIdAndDelStatus(component);
                }
            } else if(dto.getFileSource() == 2) {
                //资料
                DigitalizeResourceLibrary library = digitalizeResourceLibraryService.getByIdAndDelStatus(dto.getId());
                if (library == null) {
                    continue;
                }
                if (library.getParentId() == 0) {
                    //根目录
                    library.setDelStatus(0);
                    library.setUpdateBy(UserUtils.getUser().getId());
                    digitalizeResourceLibraryService.updateByIdAndDelStatus(library);
                } else {
                    //子目录 判断上级文件是否存在
                    DigitalizeResourceLibrary parentComponent = digitalizeResourceLibraryService.getById(library.getParentId());
                    if (ObjectUtils.isEmpty(parentComponent)) {
                        library.setParentId(0);
                    }
                    library.setDelStatus(0);
                    library.setUpdateBy(UserUtils.getUser().getId());
                    library.setUpdateDate(new Date());
                    digitalizeResourceLibraryService.updateByIdAndDelStatus(library);
                }
            } else {
                //模型集成
                DigitalizeComponentIntegration integration = new DigitalizeComponentIntegration();
                integration.setId(dto.getId().longValue());
                integration.setDelStatus(0);
                integration.setUpdateBy(UserUtils.getUser().getId());
                digitalizeComponentIntegrationService.updateByIdAndDelStatus(integration);
            }
        }
        return ResultVo.success();
    }
}
